package com.hongxun.budget.utils;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hongxun.budget.greendao.CArrivalRecordEntityDao;
import com.hongxun.budget.greendao.CBasicsWorkEntityDao;
import com.hongxun.budget.greendao.CGoodsDetailEntityDao;
import com.hongxun.budget.greendao.CGroundEntityDao;
import com.hongxun.budget.greendao.CLinePitEntityDao;
import com.hongxun.budget.greendao.CMiddleCheckEntityDao;
import com.hongxun.budget.greendao.CPolePitEntityDao;
import com.hongxun.budget.greendao.CScheduleEntityDao;
import com.hongxun.budget.greendao.CScheduleReportEntityDao;
import com.hongxun.budget.greendao.CStackPointEntityDao;
import com.hongxun.budget.greendao.DaoSession;
import com.hongxun.greenentity.sg.CArrivalRecordEntity;
import com.hongxun.greenentity.sg.CBasicsWorkEntity;
import com.hongxun.greenentity.sg.CGoodsDetailEntity;
import com.hongxun.greenentity.sg.CGroundEntity;
import com.hongxun.greenentity.sg.CLinePitEntity;
import com.hongxun.greenentity.sg.CMiddleCheckEntity;
import com.hongxun.greenentity.sg.CPolePitEntity;
import com.hongxun.greenentity.sg.CScheduleEntity;
import com.hongxun.greenentity.sg.CScheduleReportEntity;
import com.hongxun.greenentity.sg.CStackPointEntity;
import com.hongxun.greenentity.tk.DPictureEntity;
import com.hongxun.interfaces.NovaCallback;
import com.hongxun.net.ProjectNet;
import com.hongxun.net.SgProjectNet;

import java.util.List;

import dmax.dialog.SpotsDialog;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class SgXUtils {
    //--------------------NEW----------------//

    /**
     * 处理项目进度数据
     *
     * @param obj
     * @param c
     */
    public static void dealProjectProgress(Context context, JSONObject obj, final NovaCallback c, final int calli) {
        final String uuid = String.valueOf(SPUtils.get(context, SgStrKey.uuidxm, ""));
        final SpotsDialog spotsDialog = new SpotsDialog(context, "正在缓存数据...");
        spotsDialog.show();
        Observable.just(obj).subscribeOn(Schedulers.io()).map(new Func1<JSONObject, Boolean>() {
            @Override
            public Boolean call(JSONObject obj) {
                //清除项目相关数据
                delProjectProgress(uuid);
                //插入项目相关数据
                DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
                //1
                List<CScheduleEntity> list1 = JSON.parseArray(obj.getString("cScheduleList"), CScheduleEntity.class);
                for (CScheduleEntity entity : list1) {
                    entity.setSDatastate(SgStrKey.ytb);
                    session.getCScheduleEntityDao().insertOrReplace(entity);
                }
                //2
                List<CScheduleReportEntity> list2 = JSON.parseArray(obj.getString("cScheduleReportList"), CScheduleReportEntity.class);
                for (CScheduleReportEntity entity : list2) {
                    entity.setSRDatastate(SgStrKey.ytb);
                    session.getCScheduleReportEntityDao().insertOrReplace(entity);
                }
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                spotsDialog.dismiss();
                spotsDialog.cancel();
                c.success(calli, null);
            }
        });
    }

    /**
     * 处理项目照片数据
     *
     * @param obj
     * @param c
     */
    public static void dealProjectPhotos(Context context, JSONObject obj, final NovaCallback c, final int calli) {
        final SpotsDialog spotsDialog = new SpotsDialog(context, "正在缓存照片数据...");
        spotsDialog.show();
        Observable.just(obj).subscribeOn(Schedulers.io()).map(new Func1<JSONObject, Boolean>() {
            @Override
            public Boolean call(JSONObject obj) {
                //清除照片数据
                delProjectPhotos();
                //插入照片数据
                DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
                List<DPictureEntity> list3 = JSON.parseArray(obj.getString("dPictureEntityList"), DPictureEntity.class);
                for (DPictureEntity entity : list3) {
                    entity.setPDatastate(SgStrKey.ytb);
                    session.getDPictureEntityDao().insertOrReplace(entity);
                }
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                spotsDialog.dismiss();
                spotsDialog.cancel();
                c.success(calli, null);
            }
        });
    }

    /**
     * 清除项目进度及其相关数据
     */
    public static void delProjectProgress(String PGuid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
//        session.getCScheduleEntityDao().queryBuilder().where(CScheduleEntityDao.Properties.PGuid.eq(PGuid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getCScheduleReportEntityDao().queryBuilder().where(CScheduleReportEntityDao.Properties.PGuid.eq(PGuid)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    /**
     * 清除项目照片数据
     */
    public static void delProjectPhotos() {
        if (SgProjectNet.photosAll.isEmpty()) return;
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        session.getDPictureEntityDao().deleteInTx(SgProjectNet.photosAll);
    }

    /**
     * 处理材料到场数据
     *
     * @param obj
     * @param c
     */
    public static void dealProjectPresent(Context context, JSONObject obj, final NovaCallback c, final int calli) {
        final String uuid = String.valueOf(SPUtils.get(context, SgStrKey.uuidxm, ""));
        final SpotsDialog spotsDialog = new SpotsDialog(context, "正在缓存数据...");
        spotsDialog.show();
        Observable.just(obj).subscribeOn(Schedulers.io()).map(new Func1<JSONObject, Boolean>() {
            @Override
            public Boolean call(JSONObject obj) {
                //清除项目相关数据
                delProjectPresent(uuid);
                //插入项目相关数据
                DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
                //1
                List<CStackPointEntity> list1 = JSON.parseArray(obj.getString("cStackPointEntityList"), CStackPointEntity.class);
                for (CStackPointEntity entity : list1) {
                    entity.setSPDatastate(SgStrKey.ytb);
                    session.getCStackPointEntityDao().insertOrReplace(entity);
                }
                //2
                List<CArrivalRecordEntity> list2 = JSON.parseArray(obj.getString("cArrivalRecordEntityList"), CArrivalRecordEntity.class);
                for (CArrivalRecordEntity entity : list2) {
                    entity.setARDatastate(SgStrKey.ytb);
                    session.getCArrivalRecordEntityDao().insertOrReplace(entity);
                }
                //3
                List<CGoodsDetailEntity> list3 = JSON.parseArray(obj.getString("cGoodsDetailEntityList"), CGoodsDetailEntity.class);
                for (CGoodsDetailEntity entity : list3) {
                    entity.setGDDatastate(SgStrKey.ytb);
                    session.getCGoodsDetailEntityDao().insertOrReplace(entity);
                }
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                spotsDialog.dismiss();
                spotsDialog.cancel();
                c.success(calli, null);
            }
        });
    }

    /**
     * 清除材料到场及其相关数据
     */
    public static void delProjectPresent(String PGuid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        session.getCStackPointEntityDao().queryBuilder().where(CStackPointEntityDao.Properties.PGuid.eq(PGuid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getCArrivalRecordEntityDao().queryBuilder().where(CArrivalRecordEntityDao.Properties.PGuid.eq(PGuid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getCGoodsDetailEntityDao().queryBuilder().where(CGoodsDetailEntityDao.Properties.PGuid.eq(PGuid)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    /**
     * 处理中间检查数据
     *
     * @param obj
     * @param c
     */
    public static void dealProjectMiddleCheck(Context context, JSONObject obj, final NovaCallback c, final int calli) {
        final String uuid = String.valueOf(SPUtils.get(context, SgStrKey.uuidxm, ""));
        final SpotsDialog spotsDialog = new SpotsDialog(context, "正在缓存数据...");
        spotsDialog.show();
        Observable.just(obj).subscribeOn(Schedulers.io()).map(new Func1<JSONObject, Boolean>() {
            @Override
            public Boolean call(JSONObject obj) {
                //清除项目相关数据
                delProjectMiddleCheck(uuid);
                //插入项目相关数据
                DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
                //1
                List<CMiddleCheckEntity> list1 = JSON.parseArray(obj.getString("cMiddleCheckEntityList"), CMiddleCheckEntity.class);
                for (CMiddleCheckEntity entity : list1) {
                    entity.setMCDatastate(SgStrKey.ytb);
                    session.getCMiddleCheckEntityDao().insertOrReplace(entity);
                }
                //2
                List<CPolePitEntity> list2 = JSON.parseArray(obj.getString("cPolePitEntityList"), CPolePitEntity.class);
                for (CPolePitEntity entity : list2) {
                    entity.setPPDatastate(SgStrKey.ytb);
                    session.getCPolePitEntityDao().insertOrReplace(entity);
                }
                //3
                List<CLinePitEntity> list3 = JSON.parseArray(obj.getString("cLinePitEntityList"), CLinePitEntity.class);
                for (CLinePitEntity entity : list3) {
                    entity.setLPDatastate(SgStrKey.ytb);
                    session.getCLinePitEntityDao().insertOrReplace(entity);
                }
                //4
                List<CGroundEntity> list4 = JSON.parseArray(obj.getString("cGroundEntityList"), CGroundEntity.class);
                for (CGroundEntity entity : list4) {
                    entity.setGDatastate(SgStrKey.ytb);
                    session.getCGroundEntityDao().insertOrReplace(entity);
                }
                //5
                List<CBasicsWorkEntity> list5 = JSON.parseArray(obj.getString("cBasicsWorkEntityList"), CBasicsWorkEntity.class);
                for (CBasicsWorkEntity entity : list5) {
                    entity.setBWDatastate(SgStrKey.ytb);
                    session.getCBasicsWorkEntityDao().insertOrReplace(entity);
                }
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                spotsDialog.dismiss();
                spotsDialog.cancel();
                c.success(calli, null);
            }
        });
    }

    /**
     * 清除中间检查及其相关数据
     */
    public static void delProjectMiddleCheck(String PGuid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        session.getCMiddleCheckEntityDao().queryBuilder().where(CMiddleCheckEntityDao.Properties.PGuid.eq(PGuid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getCPolePitEntityDao().queryBuilder().where(CPolePitEntityDao.Properties.PGuid.eq(PGuid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getCLinePitEntityDao().queryBuilder().where(CLinePitEntityDao.Properties.PGuid.eq(PGuid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getCGroundEntityDao().queryBuilder().where(CGroundEntityDao.Properties.PGuid.eq(PGuid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getCBasicsWorkEntityDao().queryBuilder().where(CBasicsWorkEntityDao.Properties.PGuid.eq(PGuid)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    //--------------------NEW----------------//
}
